import { createRouter, createWebHistory } from 'vue-router'
import { useUser } from '@/stores'
/*
 类型申明 
    Ts自带
    插件  有不管，没有  安装 @types/插件名
    js文件，需要自己定义
*/
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'

/*
vue2
mode:'hash/history/abstract'
有什么区别
hash：地址上有#号 hash值改变  hashChange 事件监听        location.hash
history：地址上没#号 通过 pushState修改history   popState 事件监听  location.pathname,服务器要有相应配制
vue3:
history:    createWebHistory()代表history      createWebHashHistory()代表hash模式 
import.meta.env.BASE_URL读取了vite.config.ts内了base参数
服务器访问地址希望是：  www.baidu.com/xxx/login     www.baidu.com/xxx/home 
base:'/xxx'
vue3环境变量： import.meta.env   vue2环境变量:process.env

使用：
  vue文件中
     vue2:
        this.$router.push()
        this.$route
     vue3
        import {useRouter} from 'vue-router'
        const router=useRouter()
        router====this.$router
       
        import {useRoute} from 'vue-router'
        const route=useRoute()
        route====this.$route

  ts文件中，与vue2的js中使用无区别
      import router from '@/router'
      router.push('/')
*/

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/playground',
      name: 'Playground',
      component: () => import('@/views/PlayGround/index.vue'),
      // .vue不可省略   .ts一定要省  设计如此
      meta: {
        title: '演示页面'
      }
    },
    // {
    //   path: '/playtest',
    //   name: 'Playtest',
    //   component: () => import('@/views/PlayGround/playTest.vue'),
    //   meta: {}
    // },
    {
      path: '/login',
      name: 'Login',
      component: () => import('@/views/login/index.vue'),
      meta: {
        title: '登录'
      }
    },
    {
      path: '/',
      name: 'Layout',
      component: () => import('@/views/layout/index.vue'),
      redirect: '/home',
      children: [
        {
          path: '/home',
          component: () => import('@/views/home/index.vue'),
          meta: {
            title: '首页'
          }
        },
        {
          path: '/article',
          component: () => import('@/views/article/index.vue'),
          meta: {
            title: '健康百科'
          }
        },
        {
          path: '/notify',
          component: () => import('@/views/notify/index.vue'),
          meta: {
            title: '消息中心'
          }
        },
        {
          path: '/user',
          component: () => import('@/views/user/index.vue'),
          meta: {
            title: '我的'
          }
        }
      ]
    },
    {
      path: '/user/patient',
      component: () => import('@/views/user/patient-page.vue'),
      meta: { title: '家庭档案' }
    },
    {
      path: '/consult/fast',
      component: () => import('@/views/consult/consult-fast.vue'),
      meta: { title: '极速问诊' }
    },
    {
      path: '/consult/dep',
      component: () => import('@/views/consult/consult-dep.vue'),
      meta: { title: '选择科室' }
    },
    {
      path: '/consult/illness',
      component: () => import('@/views/consult/consult-illness.vue'),
      meta: { title: '病情描述' }
    },
    {
      path: '/consult/pay',
      component: () => import('@/views/consult/consult-pay.vue'),
      meta: { title: '问诊支付' }
    },
    {
      path: '/room',
      component: () => import('@/views/room/index.vue'),
      meta: { title: '问诊室' }
    },
    {
      path: '/user/consult',
      component: () => import('@/views/user/consult-page.vue'),
      meta: { title: '问诊记录' }
    },
    {
      path: '/order/pay',
      component: () => import('@/views/order/order-pay.vue'),
      meta: { title: '药品支付' }
    },
    {
      path: '/order/pay/result',
      component: () => import('@/views/order/order-pay-result.vue'),
      meta: { title: '药品支付结果' }
    },
    {
      path: '/order/:id',
      component: () => import('@/views/order/order-detail.vue'),
      meta: { title: '药品订单详情' }
    },
    {
      path: '/order/logistics/:id',
      component: () => import('@/views/order/order-logistics.vue'),
      meta: { title: '物流详情' }
    },
    {
      path: '/login/callback',
      component: () => import('@/views/login/login-callback.vue'),
      meta: { title: 'QQ登录-绑定手机' }
    }
  ]
})

// 导航守卫
// 前置守卫
// （使用return 前置条件是没有next）不写任何return 代表正常进入
// return 值（路由信息）

/*
  nprogress
 1: yarn add nprogress   yarn add @types/nprogress -D
 2:导入   import NProgress from 'nprogress'  import 'nprogress/nptogress.css'
 3：
   NProgress.configure({ showSpinner: false })
    NProgress.start()
    NProgress.done()
*/
// NProgress.configure({ showSpinner: false })
const whitePage = ['/login', '/login/callback']
router.beforeEach(to => {
  NProgress.start()
  // 如果没有登陆，且不在白名单页面，跳转到登陆页
  const store = useUser()
  // (store.userInfo===unll || store.userInfo===undefined)?undefined:store.userInfo.token
  if (!store.userInfo?.token && !whitePage.includes(to.path.toLowerCase())) {
    return '/login?redirect=' + to.fullPath
  }
})

// 后置守卫
router.afterEach(to => {
  NProgress.done()
  // console.log(to)
  // document.title = import.meta.env.VITE_APP_TITLE + '-' + to.meta.title
})

export default router
